<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增库存盘点明细')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-detail-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">盘点ID：</label>
                    <div class="col-sm-8">
                        <input name="checkId" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">盘点单号：</label>
                    <div class="col-sm-8">
                        <input id="checkCode" class="form-control" type="text" readonly>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">商品ID：</label>
                    <div class="col-sm-8">
                        <input name="productId" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">商品名称：</label>
                    <div class="col-sm-8">
                        <input id="productName" class="form-control" type="text" readonly>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">系统库存数量：</label>
                    <div class="col-sm-8">
                        <input name="systemQuantity" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">实际盘点数量：</label>
                    <div class="col-sm-8">
                        <input name="actualQuantity" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">差异数量：</label>
                    <div class="col-sm-8">
                        <input name="difference" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">调整数量：</label>
                    <div class="col-sm-8">
                        <input name="adjustQuantity" class="form-control" type="text" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">备注：</label>
                    <div class="col-sm-8">
                        <textarea name="remark" class="form-control"></textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">创建者：</label>
                    <div class="col-sm-8">
                        <input name="createdBy" class="form-control" type="text">
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">创建时间：</label>
                    <div class="col-sm-8">
                        <div class="input-group date">
                            <input name="createdTime" class="form-control" placeholder="yyyy-MM-dd" type="text">
                            <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <script th:inline="javascript">
        var prefix = ctx + "stock/detail"
        $("#form-detail-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-detail-add').serialize());
            }
        }

        $("input[name='createdTime']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });
        
        // 商品ID输入框失去焦点时，自动获取商品名称
        $("input[name='productId']").on('blur', function() {
            var productId = $(this).val();
            if (productId) {
                $.ajax({
                    url: prefix + "/getProductName/" + productId,
                    type: "get",
                    dataType: "json",
                    success: function(res) {
                        if (res.code == 0 && res.data) {
                            $("#productName").val(res.data);
                        } else {
                            $("#productName").val("未找到商品");
                            $.modal.msgWarning("商品名称获取失败");
                        }
                    },
                    error: function() {
                        $("#productName").val("未找到商品");
                        $.modal.msgWarning("商品名称获取失败");
                    }
                });
            }
        });
        
        // 盘点ID输入框失去焦点时，自动获取盘点单号
        $("input[name='checkId']").on('blur', function() {
            var checkId = $(this).val();
            if (checkId) {
                $.ajax({
                    url: prefix + "/getCheckCode/" + checkId,
                    type: "get",
                    dataType: "json",
                    success: function(res) {
                        if (res.code == 0 && res.data) {
                            $("#checkCode").val(res.data);
                        } else {
                            $("#checkCode").val("未找到盘点单");
                            $.modal.msgWarning("盘点单号获取失败");
                        }
                    },
                    error: function() {
                        $("#checkCode").val("未找到盘点单");
                        $.modal.msgWarning("盘点单号获取失败");
                    }
                });
            }
        });
    </script>
</body>
</html>